Option Strict Off
Option Explicit On

Imports System.Reflection
Imports System.Globalization

Namespace BMLERP
	Friend Class FrmLogin
		Inherits System.Windows.Forms.Form

		'Globalization members
		Private cultureInfo As CultureInfo = System.Globalization.CultureInfo.InvariantCulture
		Private ReadOnly resourceAssembly As Assembly = Resources.ResourceInterface.AssemblyValue
		Private ReadOnly resourceFileName As String = Resources.SYSTEM.LOGIN_FORM.RESOURCES_FILE_NAME
		Private ReadOnly resourceSharedFileName As String = Resources.SYSTEM.LOGIN_FORM.RESOURCES_SHARED_FILE_NAME

#Region "Properties"

		Public Property CurrentUICulture() As CultureInfo
			Get
				Return Me.cultureInfo
			End Get
			Set(ByVal value As CultureInfo)
				If (String.Compare(Me.cultureInfo.Name, value.Name, False, _
				  System.Globalization.CultureInfo.InvariantCulture) = 0) Then
					Return
				End If

				Me.cultureInfo = value

				ApplyCurrentUICulture()	' Apply current UI culture
			End Set
		End Property

#End Region

#Region "Methods"

		Private Sub InitializeSuppotedLanguages()
			Dim langIdx As Integer = 0 ' Default is English
			If (SupportLanguages IsNot Nothing) Then
				If (DefaultLanguage IsNot Nothing) Then
					langIdx = Array.IndexOf(SupportLanguages, DefaultLanguage)
				End If
			End If

			comboBoxLanguage.SelectedIndex = langIdx
			comboBoxLanguage_SelectedIndexChanged(Me, New System.EventArgs())
		End Sub

		Private Sub ApplyCurrentUICulture()
			Me.Text = Utils.ResourceHelper.LoadString( _
			  Me.resourceAssembly, _
			  Me.resourceSharedFileName, _
			  Resources.RESXSHARED.STR_APP_TITLE, _
			  Me.CurrentUICulture)

			labelFormTitle.Text = Utils.ResourceHelper.LoadString( _
			  Me.resourceAssembly, _
			  Me.resourceFileName, _
			  Resources.SYSTEM.LOGIN_FORM.STR_LOGIN, _
			  Me.CurrentUICulture)

			labelUser.Text = Utils.ResourceHelper.LoadString( _
			  Me.resourceAssembly, _
			  Me.resourceFileName, _
			  Resources.SYSTEM.LOGIN_FORM.STR_USERNAME, _
			  Me.CurrentUICulture)

			labelPassword.Text = Utils.ResourceHelper.LoadString( _
			  Me.resourceAssembly, _
			  Me.resourceFileName, _
			  Resources.SYSTEM.LOGIN_FORM.STR_PASSWORD, _
			  Me.CurrentUICulture)

			labelWorkingDate.Text = Utils.ResourceHelper.LoadString( _
			  Me.resourceAssembly, _
			  Me.resourceFileName, _
			  Resources.SYSTEM.LOGIN_FORM.STR_WORKING_DATE, _
			  Me.CurrentUICulture)

			labelLanguage.Text = Utils.ResourceHelper.LoadString( _
			   Me.resourceAssembly, _
			  Me.resourceFileName, _
			  Resources.SYSTEM.LOGIN_FORM.STR_LANGUAGE, _
			  Me.CurrentUICulture)

			buttonExit.Text = Utils.ResourceHelper.LoadString( _
			  Me.resourceAssembly, _
			  Me.resourceSharedFileName, _
			  Resources.RESXSHARED.STR_EXIT, _
			  Me.CurrentUICulture)
		End Sub

#End Region

#Region "Event Handler"

		Private Sub FrmLogin_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) _
		Handles MyBase.Load
			If Not InitSuccess Then
				BIENCHUNG()
			End If

			InitializeSuppotedLanguages()

			Me.txtUserName.Text = "nguyen"
			Me.Psw.Text = "12345"
		End Sub

		Private Sub comboBoxLanguage_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
		Handles comboBoxLanguage.SelectedIndexChanged
			If SupportLanguages Is Nothing Then
				Return
			End If

			Dim selectedIndex As Integer = Me.comboBoxLanguage.SelectedIndex
			Me.CurrentUICulture = New CultureInfo(SupportLanguages(selectedIndex))
		End Sub

		Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles buttonExit.Click
			'Adodc1.Recordset.Close
			Me.Close()
			End

		End Sub


		Private Sub FrmLogin_Activated(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Activated
			txtUserName.Focus()
			' Dim sDate As String = VB6.Format(Date.Now, "DD/MM/YYYY hh:mm:ss")
			' Dim dtDate As Date = CDate(sDate)
			m_WorkingDate.Text = VB6.Format(Today, "dd/mm/yyyy")
			WorkingDate = CDate(m_WorkingDate.Text)
		End Sub

		Private Sub m_WorkingDate_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles m_WorkingDate.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			On Error GoTo trove
			If KeyAscii = 13 Then
				WorkingDate = CDate(VB6.Format(m_WorkingDate.Text, "dd/mm/yyyy hh:mm:ss"))
				Me.Hide()

			End If
trove:
			If Err.Number > 0 Then
				MsgBox("Nhap lai ngay")
				m_WorkingDate.Focus()
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub psw_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles Psw.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			On Error GoTo Err_action

			If KeyAscii = 13 Then
				DBconn = New ADODB.Connection
				DBconn.ConnectionTimeout = 10

				ConnectString = "Provider=SQLOLEDB;Data Source=" & ServerName & ";Integrated Security=SSPI;Initial Catalog=" & DBName
				'ConnectString = "Provider=SQLOLEDB;Driver={SQL Server};Server=" & ServerName & ";" & "Database=" & DBName & ";UID=" & txtUserName.Text & ";PWD=" & Psw.Text
				'ConnectString = "Provider=SQLOLEDB;Driver={SQL Server};Server=" & ServerName & ";" & "Database=" & DBName & ";UID=sa;PWD=791935"
				DBconn.ConnectionString = ConnectString
				DBconn.Open()
				'Ket noi thanh cong
				If DBconn.State = ADODB.ObjectStateEnum.adStateOpen Then
					Adodc1.ConnectionString = ConnectString
					Adodc1.ConnectionTimeout = 40
					Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdText
					Adodc1.RecordSource = "Select Users.* from Users where UserName='" & txtUserName.Text & "'"
					Adodc1.Refresh()
					If Adodc1.Recordset.RecordCount < 1 Then
						MsgBox("Chua thiet lap menu cho user nay")
						DBconn.Close()
						txtUserName.Text = ""
						Psw.Text = ""
						txtUserName.Focus()
					Else
						usercodeLogin = Adodc1.Recordset.Fields("usercode").Value
						UserLogin = Adodc1.Recordset.Fields("UserName").Value
						TenLogin = Adodc1.Recordset.Fields("Name").Value
						NhomLogin = Adodc1.Recordset.Fields("UserGroup").Value

						Adodc1.Recordset.Close()
						Me.Tag = UserLogin
						m_WorkingDate.Focus()
					End If
				Else
					MsgBox("Username hay password khong dung")
					Psw.Text = ""
					Psw.Focus()
					GoTo EventExitSub
				End If
			End If

Next_Action:
			GoTo EventExitSub
Err_action:
			MsgBox("Username hay password khong dung")
			Psw.Text = ""
			Psw.Focus()
			Resume Next_Action

EventExitSub:
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtUserName_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtUserName.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			On Error GoTo Err_action

			If KeyAscii = 13 Then
				DBconn = New ADODB.Connection
				DBconn.ConnectionTimeout = 10
				ConnectString = "Provider=SQLOLEDB;Driver={SQL Server};Server=" & ServerName & ";" & "Database=" & DBName & ";UID=" & txtUserName.Text & ";PWD=" & Psw.Text
				DBconn.ConnectionString = ConnectString
				DBconn.Open()
				'Ket noi thanh cong
				If DBconn.State = ADODB.ObjectStateEnum.adStateOpen Then
					Adodc1.ConnectionString = ConnectString
					Adodc1.ConnectionTimeout = 40
					Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdText
					Adodc1.RecordSource = "Select Users.* from Users where UserName='" & txtUserName.Text & "'"
					Adodc1.Refresh()
					If Adodc1.Recordset.RecordCount < 1 Then
						MsgBox("Chua thiet lap menu cho user nay")
						DBconn.Close()
						txtUserName.Text = ""
						Psw.Text = ""
						txtUserName.Focus()
					Else
						usercodeLogin = Adodc1.Recordset.Fields("usercode").Value
						UserLogin = Adodc1.Recordset.Fields("UserName").Value
						TenLogin = Adodc1.Recordset.Fields("Name").Value
						NhomLogin = Adodc1.Recordset.Fields("UserGroup").Value

						Adodc1.Recordset.Close()
						Me.Tag = UserLogin
						m_WorkingDate.Focus()
					End If
				Else
					MsgBox("Username hay password khong dung")
					Psw.Text = ""
					Psw.Focus()
					GoTo EventExitSub
				End If
			End If

Next_Action:
			GoTo EventExitSub
Err_action:
			MsgBox("Username hay password khong dung")
			Psw.Text = ""
			Psw.Focus()
			Resume Next_Action
EventExitSub:
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub m_WorkingDate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles m_WorkingDate.TextChanged

		End Sub
#End Region
	End Class
End Namespace